axios.defaults.baseURL = "https://hmajax.itheima.net";

//请求拦截器
axios.interceptors.request.use(
  function (config) {
    config.headers.Authorization = localStorage.getItem("token");
    return config;
  },
  function (error) {
    console.dir(error);
    toast(error.response);
    setTimeout(() => {
      location.href = "./login.html";
    }, 2000);
    localStorage.clear();
    return Promise.reject.error;
  }
);
//响应拦截器
axios.interceptors.response.use(
  function (config) {
    return config;
  },
  function (error) {
    if (error.response.status == "400") {
      toast(error.response.data.message);
    }

    if (error.response.status == "401") {
      toast(error.response.data.message);

      setTimeout(() => {
        location.href = "./login.html";
      }, 2000);
      localStorage.clear();
      return Promise.reject.error;
    }
  }
);

function toast(msg) {
  pxmu.toast({
    msg, //内容 不能为空
    time: 2000,
    bg: "rgba(0, 0, 0, 0.5)", //背景颜色 默认黑色
    color: "#fff", //文字颜色 默认白色
    location: "center", //居中center 顶部top 底部bottom默认
    type: "wap", //默认wap样式 可选参数：pc 入参pc时
    status: "", //可选参数 success成功 warn警告 error错误 仅在type=pc时候生效，wap时可通过自定义bg、color改变样式
  });
}

function logout() {
  document.querySelector("#logout").addEventListener("click", function () {
    pxmu
      .diaglog({
        content:{text:"确定要退出吗？",color:"#000",size:"16px",lineHeight:"20px"},
        congif: {
          animation: "slidedeg", //自定义动画 动画名详见动画文档
        },
      })
      .then(function (res) {
        if (res.btn == "right") {
          localStorage.clear();
          toast("退出成功");
          setTimeout(() => {
            location.href = "./login.html";
          }, 2000);
        } else {
          //点击了左侧按钮
        }
      });
  });
}
logout();
